package com.github.binarywang.demo.wx.mp.handler;

import com.github.binarywang.demo.wx.mp.builder.TextBuilder;
import com.github.binarywang.demo.wx.mp.redmaple.dao.WeChatKeyWords;
import com.github.binarywang.demo.wx.mp.redmaple.dao.WeChatKeywordsDao;
import com.github.binarywang.demo.wx.mp.utils.HttpClientUtil;
import com.github.binarywang.demo.wx.mp.utils.RegexUtils;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Component
public class MsgHandler extends AbstractHandler {

    private static String wx_default_message = "亲，您的请求我们正在处理，请稍后再试...";
    private static String xztq_weather_url = "https://api.seniverse.com/v3/weather/now.json?key=S8TbuoPtRvRLeHA3B&location=####&language=zh-Hans&unit=c";
    private static String phone_verifcation_message = "【南极电商】亲，你的验证码是【####】，打死都不能告诉别人哦！";

    @Autowired
    private WeChatKeywordsDao weChatKeywordsDao;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService weixinService,
                                    WxSessionManager sessionManager) {

        if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) {
            //TODO 可以选择将消息保存到本地
        }

        //当用户输入关键词如“你好”，“客服”等，并且有客服在线时，把消息转发给在线客服
        try {
            if (StringUtils.startsWithAny(wxMessage.getContent(), "你好", "客服")) {
                return WxMpXmlOutMessage.TRANSFER_CUSTOMER_SERVICE()
                    .fromUser(wxMessage.getToUser())
                    .toUser(wxMessage.getFromUser()).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //TODO 组装回复消息
        //String content = "收到信息内容：" + JsonUtils.toJson(wxMessage);
        String content = wxMessage.getContent();
        // 1.先匹配数据库
        if (StringUtils.isNotBlank(content)) {
            WeChatKeyWords weChatKeyWords = weChatKeywordsDao.getbyName(content);
            if (weChatKeyWords != null) {
                content = weChatKeyWords.getKeywordValue();
                return new TextBuilder().build(content, wxMessage, weixinService);
            }
        }
        // 2.验证是否是手机号
        if (RegexUtils.checkMobile(content)) {
            // 如果发送消息为手机号码类型，则发送短信验证码
            int registCode = registCode();
            content = phone_verifcation_message.replace("####", String.valueOf(registCode));
            return new TextBuilder().build(content, wxMessage, weixinService);
        }

        // 3.调用第三方天气预报
        String new_xztq_weather_url = xztq_weather_url.replace("####", content);
        System.out.println(new_xztq_weather_url);
        content = HttpClientUtil.doGet(new_xztq_weather_url);
        if (StringUtils.isNotBlank(content)) {
            return new TextBuilder().build(content, wxMessage, weixinService);
        }
        // 默认信息
        content = wx_default_message;

        return new TextBuilder().build(content, wxMessage, weixinService);
    }

    private int registCode() {
        int registCode = (int) (Math.random() * 900000 + 100000);
        return registCode;
    }

}
